package com.hangwei.paperless;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.ImageView;

import com.hangwei.paperless.fragment.PicFragment;
import com.hangwei.paperless.widget.ViewPagerFixed;
import com.twelve.all.base.BaseActivity;
import com.twelve.all.net.API;

import java.util.ArrayList;
import java.util.List;

import me.relex.photodraweeview.OnPhotoTapListener;
import me.relex.photodraweeview.OnViewTapListener;
import me.relex.photodraweeview.PhotoDraweeView;

/**
 * Created by 张龙臣 on 2020/12/4.
 */

public class BigPicActivity extends BaseActivity {

    private ViewPagerFixed mViewPager;
    private PicAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_pic);
        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
        mViewPager = findViewById(R.id.viewPager);
        ArrayList<String> list = getIntent().getStringArrayListExtra("list");
        int position = getIntent().getIntExtra("position", 0);
        mAdapter = new PicAdapter(list, getSupportFragmentManager()) {
            @Override
            protected void goNext(int position) {
                mViewPager.setCurrentItem(position + 1, true);
            }

            @Override
            protected void goPrevious(int position) {
                mViewPager.setCurrentItem(position - 1, true);
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(position, false);
    }

    abstract static class PicAdapter extends FragmentPagerAdapter {

        private List<String> mList;

        public PicAdapter(List<String> list, FragmentManager childFragmentManager) {
            super(childFragmentManager);
            mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Fragment getItem(final int position) {
            PicFragment picFragment = new PicFragment();
            picFragment.setViewInit(new PicFragment.ViewInit() {
                @Override
                public void initView(View inflate) {
                    ImageView ivLeft = inflate.findViewById(R.id.ivLeft);

                    ImageView ivRight = inflate.findViewById(R.id.ivRight);
                    PhotoDraweeView sdv = inflate.findViewById(R.id.sdv);
                    ivLeft.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
                    ivRight.setVisibility(position == mList.size() - 1 ? View.INVISIBLE : View.VISIBLE);
                    sdv.setOnPhotoTapListener(new OnPhotoTapListener() {
                        @Override
                        public void onPhotoTap(View view, float x, float y) {

                        }
                    });
                    sdv.setOnViewTapListener(new OnViewTapListener() {
                        @Override
                        public void onViewTap(View view, float x, float y) {

                        }
                    });
                    sdv.setPhotoUri(Uri.parse(API.BASE_URL + mList.get(position)));
//                    sdv.setPhotoUri(Uri.parse("res:///"+R.drawable.server_pic_1));
                    ivLeft.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goPrevious(position);
                        }
                    });
                    ivRight.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goNext(position);
                        }
                    });
                }
            });
            return picFragment;
        }

        protected abstract void goNext(int position);

        protected abstract void goPrevious(int position);

        public void setData(ArrayList<String> data) {
            mList = data;
            notifyDataSetChanged();
        }
    }
}
